/**
 * 
 */
package com.taobao.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.taobao.client.buyer.BoughtItemList;
import com.taobao.client.buyer.SellerItemList;
import com.taobao.client.obj.TopItem;
import com.taobao.client.obj.TopRefund;
import com.taobao.client.obj.TopTrade;
import com.taobao.client.util.PopBox;

/**
 * A composite that displays the contents of the buyer's page.
 * It's assumed that the buyer's page is initialized only once. 
 * So the singleton design pattern is applied.
 * 
 * @author hujingfei.pt@taobao.com
 */
public class BuyerPage extends Composite {
		
	// The single object of this class
	private static BuyerPage singleBuyerPage;		
	public final static String HISTORY_ITEM = "BuyerPage";	
	// Call back object when getting the list of trades
	public static AsyncCallback<List<TopTrade>> boughtTradeCallBack;
	// Call back object when getting the list of refunds
	public static AsyncCallback<List<TopRefund>> getRefundCallBack;
	
	// d contains session's information
	private Data d;	
	// The main panel
	private DockPanel mainPanel = new DockPanel();	
	// Useful links
	private HTML searchLink = new HTML("<a href='javascript:;'>搜索商品并购买</a>", true);
	// A text box for buyer to enter the seller's name
	private TextBox sellerNameTextBox;
	// A vertical panel which displays the buyer's trades
	private VerticalPanel boughtTradePanel = new BoughtItemList();	
	// A vertical panel which displays the seller's items
	private VerticalPanel sellerItemPanel = new SellerItemList();
		
		
	/**
	 * The overall entry point for outer classes.
	 *  
	 * @param d
	 * @return The single object of this class
	 */
	public static BuyerPage getInstance(Data d) {
		if (d != null) {
			if (singleBuyerPage == null) {
				singleBuyerPage = new BuyerPage(d);
			}
		}
		return singleBuyerPage;
	}
	
	/**
	 * Getter and setter methods
	 */
	public Data getD() {
		return d;
	}

	public void setD(Data d) {
		this.d = d;
	}	
	
	public void setBuyer(String buyerName) {
		this.d.setBuyer(buyerName);
	}
	
	public VerticalPanel getBoughtTradePanel() {
		return boughtTradePanel;
	}

	public void setBoughtTradePanel(VerticalPanel boughtTradePanel) {
		this.boughtTradePanel = boughtTradePanel;
	}
	
	public VerticalPanel getSellerItemPanel() {
		return sellerItemPanel;
	}
	
	public void setSellerItemPanel(VerticalPanel sellerItemPanel) {
		this.sellerItemPanel = sellerItemPanel;
	}		
	
	/**
	 * Constructor.
	 * Any important initializing work goes here.
	 * 
	 * @param d
	 */
	private BuyerPage(final Data d){
		
		this.d = d;	
		
		final VerticalPanel center = new VerticalPanel();
		center.add(boughtTradePanel);
		
		FlexTable layout = new FlexTable();		
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
		
		layout.setHTML(0, 0, "输入卖家名字:");
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		
		sellerNameTextBox = new TextBox(); 
//		sellerNameTextBox.setText(INITIAL_SELLER_NAME);			
		layout.setWidget(1, 0, sellerNameTextBox);
		layout.setWidget(1, 1, searchLink);		
		
		DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    
	    // Display the trade list.
	    HTML tradeListHTML = new HTML("<a href='javascript:;'>点击查看我的交易</a>", true);
	    tradeListHTML.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				center.clear();
				center.add(boughtTradePanel);				
			}
		});
	    
	    VerticalPanel actionsVP = new VerticalPanel();
	    actionsVP.add(decPanel);
	    actionsVP.add(tradeListHTML);
	    
	    mainPanel.add(center, DockPanel.CENTER);
		mainPanel.add(actionsVP, DockPanel.WEST);
	
		boughtTradeCallBack = new AsyncCallback<List<TopTrade>>() 
		{
			@Override
			public void onFailure(Throwable arg0) {
				PopBox.error("获得订单出错", arg0.getLocalizedMessage());							
			}

			@Override
			public void onSuccess(List<TopTrade> arg0) {
				// Set the seller field of the Data object
				d.setSeller(sellerNameTextBox.getText());			
				// Clear the panel for the updated trade list
				boughtTradePanel.clear();						
				// Make sure the arg0 parameter is not null
				if (arg0 != null && arg0.size() > 0) {
					((BoughtItemList) boughtTradePanel).initialize(arg0, d);	
					// Get a list of refunds
					RPC.getService().getBuyerRefunds(d.getBuyer(), getRefundCallBack);
				} else {
					PopBox.error("Get Trade Information", "No Trades");
				}
			}
	
		};				
		RPC.getService().tradeBoughtGet(d.getBuyer(), boughtTradeCallBack);	
		
		// initialize getRefundCallBack
		getRefundCallBack = new AsyncCallback<List<TopRefund>>() {
			
			@Override
			public void onSuccess(List<TopRefund> result) {
				if (result != null && result.size() > 0) {
					((BoughtItemList) boughtTradePanel).setTrList(result);
					((BoughtItemList) boughtTradePanel).update();
				}				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				PopBox.error("获得退款失败", caught.getLocalizedMessage());				
			}
		};
		
		searchLink.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent arg0) {
				center.clear();
				center.add(sellerItemPanel);
				querySellerItem();				
			}
			
		});
		initWidget(mainPanel);		
	}

	/**
	 * Search the seller's items.
	 */
	private void querySellerItem() {		
		RPC.getService().itemsGet(sellerNameTextBox.getText(), 
		new AsyncCallback<List<TopItem>>()
		{
				@Override
				public void onFailure(Throwable arg0) {
					PopBox.error("查询商品出错", arg0.getLocalizedMessage());
				}
	
				@Override
				public void onSuccess(List<TopItem> arg0) {				
					d.setSeller(sellerNameTextBox.getText()); 
					sellerItemPanel.clear();
					((SellerItemList) sellerItemPanel).initialize(arg0, d);				
				}
		});
		
	}
}
